package com.lt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lt.reggie.common.BaseContext;
import com.lt.reggie.common.R;
import com.lt.reggie.entity.AddressBook;
import com.lt.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Queue;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        //将当前登录的用户的id添加到地址对象中
        addressBook.setUserId(BaseContext.getThreadLocal());
        log.info("addressBook: {}", addressBook);
        //进行添加操作
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询所有地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //获取当前登录用户的id
        addressBook.setUserId(BaseContext.getThreadLocal());
        log.info("addressBook :{}", addressBook);
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(),AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        //执行查询
        List<AddressBook> list = addressBookService.list(queryWrapper);
        log.info("list : {}", list);
        return R.success(list);
    }

    /**
     * 设置默认地址
     *
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook :{}", addressBook);
        //将当前登录的用户的所有地址的isDefault修改成0
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getThreadLocal());
        queryWrapper.set(AddressBook::getIsDefault, 0);
        //执行修改
        addressBookService.update(queryWrapper);
        //修改设置默认地址对象的isDefault位1
        addressBook.setIsDefault(1);
        //这些修改
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getThreadLocal());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (null == addressBook) {
            return R.error("没有找到对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 根据id查地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到对象");
        }
    }


    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        log.info("addressBook: {}", addressBook);
        //进行添加操作
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


}
